package com.cafe.case7.client;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.cafe.case7.request.GoodsDetailRequest;
import com.cafe.case7.request.GoodsListRequest;
import com.cafe.case7.response.GoodsDetailResponse;
import com.cafe.case7.response.GoodsListResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class JdClient {

    private String postRequest(String param) {
        return HttpUtil.createPost("https://api.m.jd.com/api").body(param).execute().body();
    }


    /**
     * 商品列表接口
     *
     * @param request
     * @return
     */
    public GoodsListResponse goodsList(GoodsListRequest request) {
        String result = postRequest(JSONUtil.toJsonStr(request));
        return JSONUtil.toBean(result, GoodsListResponse.class);
    }

    /**
     * 商品详情接口
     *
     * @param request
     * @return
     */
    public GoodsDetailResponse goodsDetail(GoodsDetailRequest request) {
        String result = postRequest(JSONUtil.toJsonStr(request));
        return JSONUtil.toBean(result, GoodsDetailResponse.class);
    }


    public static void main(String[] args) {
        JdClient jdClient = new JdClient();
        GoodsListRequest request = new GoodsListRequest();
        GoodsListResponse goodsListResponse = jdClient.goodsList(request);
        System.out.println(goodsListResponse);

        GoodsDetailRequest  goodsDetailRequest = new GoodsDetailRequest();
        GoodsDetailResponse goodsDetailResponse = jdClient.goodsDetail(goodsDetailRequest);
        System.out.println(goodsDetailResponse);
    }

}
